package com.renchunlin.taobaounion.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.renchunlin.taobaounion.R;
import com.renchunlin.taobaounion.model.domain.SelectedPageCategory;

import java.util.ArrayList;
import java.util.List;

/*
 * class title:
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/1/25.
 * PS: Not easy to write code, please indicate.
 */
public class SelectedPageLeftAdapter extends RecyclerView.Adapter<SelectedPageLeftAdapter.InnerHolder> {

    private List<SelectedPageCategory.DataBean> mData = new ArrayList<>();
    private int mCurentSelectedPosition = 0;
    private OnLeftItemClickLisener ItemClickListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selected_page_left, parent, false);
        InnerHolder innerHolder = new InnerHolder(inflate);
        return innerHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        SelectedPageCategory.DataBean dataBean = mData.get(position);
        if (mCurentSelectedPosition == position) {
            holder.left_category_tv.setBackgroundColor(holder.left_category_tv.getResources().getColor(R.color.CEFEEEE));
        } else {
            holder.left_category_tv.setBackgroundColor(holder.left_category_tv.getResources().getColor(R.color.CFFFFFF));
        }
        holder.left_category_tv.setText(dataBean.getFavorites_title());

        holder.left_category_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ItemClickListener != null && mCurentSelectedPosition != position) {
                    //修改当前选中的位置
                    mCurentSelectedPosition = position;
                    ItemClickListener.onLeftItemClick(dataBean);
                    notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(SelectedPageCategory categorys) {
        List<SelectedPageCategory.DataBean> data = categorys.getData();
        if (data != null) {
            this.mData.clear();
            this.mData.addAll(data);
            notifyDataSetChanged();
        }
        if (mData.size() > 0) {
            ItemClickListener.onLeftItemClick(mData.get(mCurentSelectedPosition));
        }
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        TextView left_category_tv;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            left_category_tv = itemView.findViewById(R.id.left_category_tv);
        }
    }

    public void setOnLeftItemClickListener(OnLeftItemClickLisener listener) {
        this.ItemClickListener = listener;
    }

    public interface OnLeftItemClickLisener {
        void onLeftItemClick(SelectedPageCategory.DataBean item);
    }
}
